var cid = 1;var todoTimer;
var clockTime = 120; //计时60s
var seriesObject = new Object(), tabId= '';;
var arr = new Array(), colorList = new Array(),styleList= new Array()
styleList.push("layui-bg-orange1");
styleList.push("layui-bg-green1");
styleList.push("layui-bg-cyan1");
styleList.push("layui-bg-blue1");
styleList.push("layui-bg-black1");
styleList.push("layui-bg-purple1");
styleList.push("layui-bg-light1");
colorList.push("#546FC6");
colorList.push("#FBB141");
colorList.push("#86B96F");
colorList.push("#67B47F");
colorList.push("#FDC937");
colorList.push("#FD7E56");
colorList.push("#ABBF56");
colorList.push("#F6963E");
colorList.push("#66A281");
colorList.push("#D34F51");
colorList.push("#61A52C");
colorList.push("#A998B0");
colorList.push("#6B907F");
colorList.push("#5E87BB");
colorList.push("#A37A6B");
colorList.push("#9C9B9D");
var isProObj = convertEnumToObj(isProList);//任务单开盘状态
var proTypeObj = convertEnumToObj(proTypeList);//任务单状态
var carTypeObj = convertEnumToObj(carTypeList);//车辆类型
var weightTypeObj = convertEnumToObj(weightTypeList);//过磅类型
layui.use(['table', 'table', 'form', 'laydate', 'element'], function() {
	var table = layui.table, $ = layui.jquery, layer = layui.layer,
		element = layui.element, form = layui.form, laydate = layui.laydate;
	//初始化查询参数
	loadData({}, '/home/load-title', 0);//加载头部数据
	setTimeout(function () {
		loadData({type:0}, '/home/load-weight', 1);//加载原材过磅数据
	}, 100);
	setTimeout(function () {
		loadData({type:1}, '/home/load-weight', 2);//加载生产过磅数据
	}, 200);
	setTimeout(function () {
		loadData({}, '/home/load-trend', 3);//加载发货、生产走势数据
	}, 300);
	setTimeout(function () {
		loadData({}, '/home/load-line', 4);//加载生产top数据
	}, 400);
	setTimeout(function () {
		loadData({}, '/home/load-car', 5);//加载车辆top数据
	}, 500);
	setTimeout(function () {
		loadData({}, '/home/load-con-progress', 6);//加载合同进度数据
	}, 600);
	setTimeout(function () {
		loadData({}, '/home/load-strength', 7);//加载强度占比数据
	}, 700);
	setTimeout(function () {
		loadData({}, '/pro/dispatch/line/producing', 8);//加载车辆排队数据
	}, 800);
    setTimeout(function () {
        loadData({}, '/pro/tran/car/queue/list', 9);//加载车辆排队数据
    }, 900);
	refreshTimer(clockTime);
	function loadTitle(data) {
		if (data != null) {
			$('.title-planStere').html(data.planStere);
			$('.title-dayStere').html(data.dayStere);
			$('.title-monthStere').html(data.monthStere);
			$('.title-yearStere').html(data.yearStere);
		}
	}
	function loadData(options, url, type) {
		// 0 加载头部数据 1 加载原材过磅数据 2 加载生产过磅数据 3 加载发货、生产走势数据 4 加载生产top数据
		// 	5 加载车辆top数据 6 加载合同进度数据 7 加载强度占比数据 8 加载车辆排队数据
		$.ajax({
			type: "POST",
			url: url,
			async: true,
			dataType: 'json',
			timeout: 5000,
			data: options,
			success: function(res) {
				// layer.close(loading);
                // console.log(type)
				// console.log(res);
				if(res.code == 0) {
					if (type == 0)
						loadTitle(res.data);
					else if (type == 1)
						initMaterialWeight(res.data);
					else if (type == 2)
						initProductionWeight(res.data);
					else if (type == 3)
						initTrend(res.data);
					else if (type == 4)
						initLineTop(res.data);
					else if (type == 5)
						initCarTop(res.data);
					else if (type == 6)
						initProgress(res.data);
					else if (type == 7)
						initStrength(res.data);
					else if (type == 8)
						initLineUp(res.data);
                    else if (type == 9)
                        initLineWait(res.data);
				} else {
					if (type == 1)
						initMaterialWeight(null);
					else if (type == 2)
						initProductionWeight(null);
					else if (type == 3)
						initTrend(null);
					else if (type == 4)
						initLineTop(null);
					else if (type == 5)
						initCarTop(null);
					else if (type == 6)
						initProgress(null);
					else if (type == 7)
						initStrength(null);
                    else if (type == 8)
                        initLineUp(null);
                    else if (type == 9)
                        initLineWait(null);
				}
			},
			error: function(res) {
				// layer.msg("请求失败，请稍后再试！");
				// layer.close(loading);
				return;
			},
			complete: function(XMLHttpRequest, status) {
				if(status == 'timeout') {
					// layer.msg("请求超时，请稍后再试！");
					// layer.close(loading);
					return;
				}
			}
		});
	}
	$('.sync-btn').on('click', function () {
        loadData({}, '/home/load-title', 0);//加载头部数据
        setTimeout(function () {
            loadData({type:0}, '/home/load-weight', 1);//加载原材过磅数据
        }, 100);
        setTimeout(function () {
            loadData({type:1}, '/home/load-weight', 2);//加载生产过磅数据
        }, 200);
        setTimeout(function () {
            loadData({}, '/home/load-trend', 3);//加载发货、生产走势数据
        }, 300);
        setTimeout(function () {
            loadData({}, '/home/load-line', 4);//加载生产top数据
        }, 400);
        setTimeout(function () {
            loadData({}, '/home/load-car', 5);//加载车辆top数据
        }, 500);
        setTimeout(function () {
            loadData({}, '/home/load-con-progress', 6);//加载合同进度数据
        }, 600);
        setTimeout(function () {
            loadData({}, '/home/load-strength', 7);//加载强度占比数据
        }, 700);
        setTimeout(function () {
            loadData({}, '/pro/dispatch/line/producing', 8);//加载车辆排队数据
        }, 800);
        setTimeout(function () {
            loadData({}, '/pro/tran/car/queue/list', 9);//加载车辆排队数据
        }, 900);
    });

	function initLineTop(data) {//初始化生产线top
		var html = '<tr><td colspan="4">暂无数据</td></tr>';
		if (data != null) {
			html = '';
			$.each(data, function (index, val) {
				var proStere = convertNumber(val.proStere);
				var pumpStere = convertNumber(val.pumpStere);
				var rate = convertNumber(val.rate);
				var defaultStyle = 'layui-bg-grey1';
                var style = 'layui-bg-orange1';
                var length = styleList.length;
                if (index < length) {
					style = styleList[index];
				} else {
					var ind = length % index;
					if (ind >= length)
						ind--;
					style = styleList[ind];
				}
                if (proStere <= 0)
                	style = defaultStyle;
				// console.log()
				html += '<tr><td><span class="layui-badge ' + style +'">' + val.name + '</span></td>\n' +
					'<td><span class="layui-badge ' + style +'">产量:' + proStere + 'm³</span></td>\n' +
					'<td><span class="layui-badge ' + style +'">泵送:' + pumpStere + 'm³</span></td>\n' +
					'<td><span class="layui-badge ' + style +'">产线使用率:' + rate + '%</span></td></tr>';
			});
		}
		$('.line-body-list').html(html);
	}
	function initCarTop(data) {//初始化车辆top
		var html = '<tr><td colspan="3">暂无数据</td></tr>';
		if (data != null) {
			html = '';
			$.each(data, function (index, val) {
				var topNum = parseInt(index + 1);
				var sendStere = convertNumber(val.sendStere);
				var pumpStere = convertNumber(val.pumpStere);
				var tranNo = convertNumber(val.tranNo);
				var distance = convertNumber(val.distance);
				var carCode = convertVal(val.carCode);
				var carNo = convertVal(val.carNo);
				var carTip = '';
				if (!isNull(carCode) && !isNull(carNo)) {
					carTip = carCode + '[' + carNo + ']';
				} else {
					carTip = carCode + carNo;
				}
				html += '<tr><td><span class="layui-badge">' + topNum + '</span></td>\n' +
					'<td class="contract-text">' + carTip + '</td>\n' +
					'<td class="contract-text">\n' +
					'<span class="layui-badge layui-bg-orange">量:' + sendStere + 'm³</span>\n' +
					'<span class="layui-badge layui-bg-blue">车:' + tranNo + '次</span>\n' +
					'<span class="layui-badge layui-bg-cyan">泵量:' + pumpStere + 'm³</span>\n' +
					'<span class="layui-badge layui-bg-green">运距:' + distance + 'km</span>\n' +
					'</td></tr>';
			});
		}
		$('.car-body-list').html(html);
	}
	function initProgress(data) {//初始化合同进度
		var html = '<tr><td colspan="6">暂无数据</td></tr>';
		if (data != null) {
			html = '';
			$.each(data, function (index, val) {
				//0 待供 1 正供 2 供毕 3 暂供
				var topNum = parseInt(index + 1);
				var style = '';
				var type = val.type;
				var successStyle= 'text-success';
				var successText= '进行中';
				var site = convertVal(val.site);
				var startTime = convertVal(val.startTime);
				var conName = convertVal(val.conName);
				var isDelay = convertNumber(val.isDelay);
				var planStere = convertNumber(val.planStere);
				var sendStere = convertNumber(val.sendStere);
				var clientName = val.clientName;
				var pumpNorm = val.pumpNorm;
				if (!isNull(clientName))
					conName = clientName + '[' + conName + ']';
				if (!isNull(pumpNorm))
					site = site + '[' + pumpNorm + ']';
				if (type == 0) {
					if (isDelay == 0) {//text-info
						successText = '未开始';
						successStyle = 'text-warning';
					} else {
						successText = '已延期';
						successStyle = 'text-danger';
					}
				} else if (type == 1) {
					successText= '进行中';
					successStyle = 'text-success';
				} else if (type == 2) {
					successText= '已完成';
					successStyle = 'text-muted';
				} else if (type == 3) {
					successText= '暂停中';
					successStyle = 'text-info';
				}
				if (type < styleList.length)
					style = styleList[type];
				else
					style = styleList[styleList.length % type];
				var conProgress = 'con-progress' + index;
				html += '<tr><td><span class="layui-badge ' + style + '">' + topNum + '</span></td>\n' +
					'<td><span class="layui-text"><a>' + conName + '</a></span></td>\n' +
					'<td align="center">' + site + '</td>\n' +
					'<td align="center">' + startTime + '</td>\n' +
					'<td align="center"><span class="' + successStyle + '">' + successText + '</span></td>\n' +
					'<td class="contract-text">\n' +
					'<div class="layui-progress" lay-showpercent="true" lay-filter="' + conProgress + '">\n' +
					'<div class="layui-progress-bar" lay-percent="' + sendStere + ' / ' + planStere + '"></div>\n' +
					'</div></td></tr>';

			});
		}
		$('.conProgress-body-list').html(html);
		if (data != null) {
            $.each(data, function (index, val) {
                var planStere = convertNumber(val.planStere);
                var sendStere = convertNumber(val.sendStere);
                var conProgress = 'con-progress' + index;
                element.progress(conProgress, sendStere + '/' + planStere);
                element.init();
            });
        }
	}
	function initMaterialWeight(data) {//初始化材料车辆top
		var html = '<tr><td colspan="3">暂无数据</td></tr>';
		if (data != null) {
			html = '';
			$.each(data, function (index, val) {
				var style = 'layui-bg-cyan';
				var successStyle = 'layui-bg-blue';
				if (index > 3) {
					style = 'layui-bg-gray';
					successStyle = 'layui-bg-green';
				}
				var topNum = parseInt(index + 1);
				var carNo = convertVal(val.carNo);
				var material = convertVal(val.material);
				var inTime = convertVal(val.inTime);
				var outTime = convertVal(val.outTime);
				var createTime = convertVal(val.createTime);
				var time = '';
				var tip = '已过毛重';
				if (outTime != null) {
					tip = '过磅已完成';
					time = outTime.substring(5,16);
				} else if (inTime != null) {
					tip = '已过毛重';
					time = inTime.substring(5,16);
				} else {
					tip = '未开始过磅';
					time = createTime.substring(5,16);
				}
				html += '<tr><td><span class="layui-badge ' + style + '">' + topNum + '</span></td>\n' +
					'<td>' + carNo + '  <span class="layui-badge ' + successStyle + '">' + material + '</span></td>\n' +
					'<td>' + time + ' ' + tip + '</td></tr>';
			});
		}
		$('.material-weight-body-list').html(html);
	}
	function initProductionWeight(data) {//初始化生产车辆top
		var html = '<tr><td colspan="3">暂无数据</td></tr>';
		if (data != null) {
			html = '';
			$.each(data, function (index, val) {
				var style = 'layui-bg-cyan';
				var successStyle = 'layui-bg-blue';
				if (index > 3) {
					style = 'layui-bg-gray';
					successStyle = 'layui-bg-green';
				}
				var topNum = parseInt(index + 1);
				var carType = convertNumber(val.carType);
				var weightType = convertNumber(val.weightType);
				var carNo = convertVal(val.carNo);
				var inTime = convertVal(val.inTime);
				var createTime = convertVal(val.createTime);
				var time = '';
				var tip = '发货过磅';
				if (carType == 4)
					tip = '水稳层过磅';
				if (weightType == 3)
					tip = '退料过磅';
				else if (weightType == 4)
					tip = '发货过磅';
				else if (weightType == 5)
					tip = '皮重过磅';
				else if (weightType == 7)
					tip = '除渣过磅';
				if (inTime != null) {
					time = inTime.substring(5,16);
				} else {
					time = createTime.substring(5,16);
				}
				html += '<tr><td><span class="layui-badge ' + style + '">' + topNum + '</span></td>\n' +
					'<td><span class="layui-badge ' + successStyle + '">' + carNo + '</span></td>\n' +
					'<td>' + time + ' ' + tip + '</td></tr>';
			});
		}
		$('.production-weight-body-list').html(html);
	}
	function initLineUp(data) {//初始化排队车辆top
		var html = '<tr><td colspan="2">暂无数据</td></tr>';
		if (data != null) {
		    html = '';
            $.each(data, function (index, val) {
                var lineName = val.lineName;
                html += '<tr><td><span class="layui-badge layui-bg-cyan">' + lineName + '</span></td>\n' +
                    '<td>\n';
                    // '<span class="layui-badge layui-bg-blue">A26 [15m³]</span>\n' +
                    // '<span class="layui-badge layui-bg-blue">A26 [15m³]</span>\n' +
                    // '<span class="layui-badge layui-bg-blue">A26 [15m³]</span>\n' +
                    // '<span class="layui-badge layui-bg-blue">A26 [15m³]</span>\n' +
                    // '<span class="layui-badge layui-bg-blue">A26 [15m³]</span>\n' +
                    // '</td></tr>';
                if (val.disList != null) {
                    $.each(val.disList, function (i, v) {
                        var carCode = v.carCode;
                        var stere = 0;
                        if (v.uploadStere != null)
                            stere = v.uploadStere;
                        html += '<span class="layui-badge layui-bg-blue" style="margin-right: 8px;">' + carCode + ' [' + stere + 'm³]</span>';
                    });
                }
                html += '</td></tr>';
            });
        }
		$('.line-send-body-list').html(html);
	}
    function initLineWait(data) {//初始化排队车辆top
        var html = '<tr><td>暂无数据</td></tr>';
        if (data != null) {
            html = '<tr><td>';
            $.each(data, function (index, val) {
                var carNum = parseInt(index + 1);
                var carCode = val.code;
                var carNo = val.carNo;
                var style = 'layui-bg-green';
                if (index < 3)
                    style = 'layui-bg-orange';
                // if (!isNull(carNo))
                //     carCode = carCode + '[' + carNo + ']';
                carCode = '(' + carNum + ') ' + carCode;
                html += '<span class="layui-badge '+ style + '" style="margin-right: 8px;">' + carCode + '</span>';

            });
            html += '</td></tr>';
        }
        $('.line-wait-body-list').html(html);
    }
	function initStrength(data) {//初始化强度占比
		var arr = [{ value: 0, name: '暂无' }];
		if (data != null) {
			arr = new Array();
			$.each(data, function (index, val) {
				var sendStere = convertNumber(val.sendStere);
				var level = convertVal(val.level);
				var strength = convertVal(val.strength);
				if (isNull(level))
					level = strength;
				var obj = {value:sendStere, name: level};
				arr[index] = obj;
			});
		}
		var strengthCharts = echarts.init(document.getElementById('strengthProportion'));
		var options1 = {
			title:{subtext:''},
			tooltip: {
				trigger: 'item',
				formatter: "{a} <br/>{b} : {c} ({d}%)"
			},
			legend: {
				top: '-1%',
				left: 'left'
			},
			series: [
				{
					name: '强度占比',
					type: 'pie',
					radius: ['40%', '70%'],
					avoidLabelOverlap: false,
					label: {
						show: false,
						position: 'center'
					},
					label: {
						show: true,
						formatter:'{b}\n {c} ({d}%)'
					},
					emphasis: {
						label: {
							show: true,
							fontSize: 40,
							fontWeight: 'bold'
						}
					},
					labelLine: {
						show: false
					},
					data: arr,
					emphasis: {
						itemStyle: {
							shadowBlur: 10,
							shadowOffsetX: 0,
							shadowColor: 'rgba(0, 0, 0, 0.5)'
						}
					}
				}
			]
		};
		strengthCharts.setOption(options1);
		// 窗口大小改变事件
		window.onresize = function () {
			strengthCharts.resize();
		};
	}
	function initTrend(data) {
		// 渲染销售额图表
		var xTipArr = ['0', '2',  '4', '6',  '8', '10', '12','14', '16', '18', '20', '22'];
		var proPumpArr = ['0', '0',  '0', '0',  '0', '0', '0','0', '0', '0', '0', '0'];
		var noProPumpArr = ['0', '0',  '0', '0',  '0', '0', '0','0', '0', '0', '0', '0'];
		var sendArr = ['0', '0',  '0', '0',  '0', '0', '0','0', '0', '0', '0', '0'];
		var signArr = ['0', '0',  '0', '0',  '0', '0', '0','0', '0', '0', '0', '0'];
		if (data != null) {
			proPumpArr = new Array();
			noProPumpArr = new Array();
			sendArr = new Array();
			signArr = new Array();
			$.each(data,function (index, val) {
				var sendStere = convertNumber(val.sendStere);
				var pumpStere = convertNumber(val.pumpStere);
				var signStere = convertNumber(val.signStere);
				var proStere = convertNumber(val.proStere);
				var noProStere = parseInt(sendStere - pumpStere);
				proPumpArr.push(pumpStere);
				sendArr.push(sendStere);
				signArr.push(signStere);
				noProPumpArr.push(noProStere);
			});
		}
		var productionTrendCharts = echarts.init(document.getElementById('productionTrend'), myEchartsTheme);
		var productionTrendOption = {
			title: {
				text: '发货方量'
			},
			tooltip: {trigger: 'axis', axisPointer: {lineStyle: {color: '#E0E0E0'}}},
			color: ['#10B4E8', '#FFA800'],
			legend: {
				selectedMode: false,
				data: ['泵送方量', '非泵送方量'], textStyle: {color: '#595959'}
			},
			grid: {top: '75px', left: '35px', right: '55px', bottom: '40px'},
			xAxis: {
				nameTextStyle: {color: '#595959'},
				type: 'category',
				data: xTipArr,
				axisLine: {lineStyle: {color: '#E0E0E0'}, symbol: ['none', 'arrow'], symbolOffset: [0, 10]},
				axisLabel: {color: '#8c8c8c'},
				axisTick: {alignWithLabel: true}
			},
			yAxis: {
				nameTextStyle: {color: '#595959'},
				type: 'value',
				boundaryGap: ['0', '20%'],
				axisTick: {show: false},
//              axisLine: {lineStyle: {color: '#E0E0E0'}, symbol: ['none', 'arrow'], symbolOffset: [0, 10]},
				axisLabel: {color: '#8c8c8c'},
				splitLine: {show: false},
				splitArea: {show: false},
				minInterval: 1
			},
			series: [{
				name: '泵送方量', type: 'bar', stack: 'one', barMaxWidth: '30px',
				label: {normal: {show: true, position: 'inside'}}
			}, {
				name: '非泵送方量', type: 'bar', stack: 'one', barMaxWidth: '30px',
				label: {normal: {show: true, position: 'inside'}}
			}]
		};
		productionTrendCharts.setOption(productionTrendOption);

		productionTrendCharts.setOption({series: [{data: proPumpArr},
				{data: noProPumpArr}]});
		// 渲染访问量图表
		var sendTrendCharts = echarts.init(document.getElementById('sendTrend'), myEchartsTheme);
		var sendTrendOption = {
			title: {
				text: '发货趋势'
			},
			tooltip: {
				trigger: 'axis'
			},
			legend: {
				data: ['发货方量', '非泵方量', '泵送方量']
			},
			grid: {
				left: '3%',
				right: '4%',
				bottom: '3%',
				containLabel: true
			},
			toolbox: {
				feature: {
					saveAsImage: {}
				}
			},
			xAxis: {
				type: 'category',
				boundaryGap: false,
				data: xTipArr
			},
			yAxis: {
				type: 'value'
			},
			series: [
				{
					name: '发货方量',
					type: 'line',
					// stack: 'Total',
					data: sendArr
				},
				{
					name: '非泵方量',
					type: 'line',
					// stack: 'Total',
					data: noProPumpArr
				},
				{
					name: '泵送方量',
					type: 'line',
					// stack: 'Total',
					data: proPumpArr
				}

			]
		};
		sendTrendCharts.setOption(sendTrendOption);
		// 切换选项卡重新渲染
		element.on('tab(tabZZT)', function (data) {
			if (data.index == 0) {
                sendTrendCharts.resize();
			} else {
                productionTrendCharts.resize();
			}
		});

		// 窗口大小改变事件
		window.onresize = function () {
			productionTrendCharts.resize();
			sendTrendCharts.resize();
		};
	}
});

function refreshTimer(time) {//定时器刷新待办事项
    time--;
    if (time <= 0) {
        time = clockTime;
        clearTimeout(todoTimer);
        $(".sync-btn").trigger("click");
        refreshTimer(time);
    } else {
        todoTimer = setTimeout('refreshTimer(' + time +')',1000);
    }
}

